package www.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import www.com.common.ListResult;
import www.com.entity.Film;
import www.com.mapper.FilmMapper;
import www.com.service.FilmService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Service
public class FilmServiceImpl extends ServiceImpl<FilmMapper, Film> implements FilmService {

    @Autowired
    private FilmMapper filmMapper;


    @Override
    public ListResult<Film> getAllFilmList(Film film, Integer pageSize, Integer currentPage) {
//        QueryWrapper<Film> queryWrapper = new QueryWrapper<>();
//        if( film.getName() != null){
//            queryWrapper.like("name", film.getName());
//        }
//        if(film.getType() != null){
//            queryWrapper.eq("type", film.getType());
//        }
//        if(film.getCountry() != null){
//            queryWrapper.eq("country", film.getCountry());
//        }
        Page<Film> page = new Page<>(currentPage, pageSize);
        List<Film> list = lambdaQuery()
                .like(film.getName() != null, Film::getName, "name")
                .eq(film.getType() != null, Film::getType, "type")
                .eq(film.getCountry() != null, Film::getCountry, "country")
                .page(page).getRecords();

        ListResult<Film> result = new ListResult<>();
        result.setList(list);
        result.setTotal(page.getTotal());
        return result;
    }

    @Override
    public Object createFilm(Film film) {
        QueryWrapper<Film> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", film.getName());
        Film doc = filmMapper.selectOne(queryWrapper);
        if (doc == null) {
            film.setId(UUID.randomUUID().toString());
            filmMapper.insert(film);
            return "";

        } else {

            throw new IllegalArgumentException("已有");
        }

    }

    @Override
    public Object updateFilm(Film film) {
       Boolean status =  updateById(film);
       if(!status){
           throw  new RuntimeException("更新失败～");
       }
        return "";
    }

    @Override
    public Object deleteFilm(String id) {
        QueryWrapper<Film> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int bol = filmMapper.deleteById(id);
        if (bol == 1) {
            return "";
        }
        throw new IllegalArgumentException("删除失败，请重试");
    }
}
